package com.example.demo.service.impl;

import com.example.demo.domain.entity.ChapterEntity;
import com.example.demo.mapper.ChapterMapper;
import com.example.demo.service.ChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ChapterServiceImpl implements ChapterService, BeanFactoryAware, BeanNameAware, BeanPostProcessor {


    @Autowired
    private ChapterMapper chapterDao;

    @Override
    @Transactional(readOnly = true)
    public ChapterEntity selectChapter(int id) {
            return chapterDao.selectChapter(id);
    }

    @Override
    @Transactional
    public void insertCapter(Long bookId, String content) {
//        chapterDao.insertCapter(bookId, content);
        //此犯法会报错，测试是否会插入成功，学习事务过程
//        chapterDao.selectTest();
        log.info("{}", chapterDao.selectTest());
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("setBeanFactory");
    }

    @Override
    public void setBeanName(String name) {
        log.info("setBeanName");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ChapterService){
            log.info("postProcessBeforeInitialization: "+beanName);
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        log.info("postProcessAfterInitialization: "+beanName);
        return  bean;
    }
}
